#!/bin/bash

## Copyright (c) 2013, PoiXson, Mattsoft
## <http://poixson.com/>
## <http://mattsoft.net/>
## 
## Permission to use, copy, modify, and/or distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.
## 
## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
## ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


source $(dirname $0)/_common.sh
GlobalY=0
title	"           Common Server Scripts            " \
	"Thank you for finding a use for my code! :-)" \
	"           by PoiXson & Mattsoft            "
newline
echo "This wizard will install PoiXson Common Server Scripts onto your linux"
echo "server. It is recommended to have an up to date version of CentOS."
while ( true) ; do
	newline
	echo "  1) Wizard - This will guide you and ask you before each step. (recommended)"
	echo "  2) Fast   - This will install everything with no further prompts. Use with caution!"
	echo "  2) Quit   - Don't need this."
	newline
	read -n1 -p "Please pick an install method. [w/f/q] " answer
	newline
	case $answer in
		1|w)
			break
		;;
		2|f)
			GlobalY=1
			answer='y'
			break
		;;
		3|q|n)
			echo "bye bye jerk"
			newline
			exit
		;;
		*)
	esac
done
newline
echo "******************************"


# aliases
newline
echo "[ Aliases ]"
if [ $GlobalY -eq 0 ]; then
	read -n1 -p "Would you like to install command aliases? [y/N] " answer
fi
newline
case $answer in
	y|Y)
		ln -s ${PXN_SCRIPTS}/aliases.sh /etc/profile.d/pxn.sh
		echo " * Installed /etc/profile.d/pxn.sh"
	;;
	*)
		echo "Skipping.."
	;;
esac
newline


# crons
newline
echo "[ Crons ]"
if [ $GlobalY -eq 0 ]; then
	read -n1 -p "Would you like to install the crons config to root? [y/N] " answer
fi
newline
case $answer in
	y|Y)
		newline
		echo "What email addresses should cron reports be sent to?"
		echo "(You may use more than one separating with a , or blank for default)"
		read -p " > " email
		if [ -z "$email" ]; then
			email="root,crons@mattsoft.net"
		fi
		echo "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin"	>  /var/spool/cron/root
		echo "MAILTO=${email}"						>> /var/spool/cron/root
		echo ""								>> /var/spool/cron/root
		echo "# yum update once a day - 0:33"				>> /var/spool/cron/root
		echo "33 0 * * * yum update -y"					>> /var/spool/cron/root
		echo ""								>> /var/spool/cron/root
		echo "# update repos every 4 hours - 4:03"			>> /var/spool/cron/root
		echo "3 */4 * * * ${PXN_SCRIPTS}/incremental.sh"		>> /var/spool/cron/root
		echo ""								>> /var/spool/cron/root
		echo "# run daily backup - 9:05"				>> /var/spool/cron/root
		echo "5 9 * * * ${PXN_SCRIPTS}/daily.sh"			>> /var/spool/cron/root
		echo ""								>> /var/spool/cron/root
		echo " * Installed /var/spool/cron/root"
		newline
		cat /var/spool/cron/root
		if [ $DEBUG -eq 0 ]; then
			service crond restart
		fi
	;;
	*)
		echo "Skipping.."
	;;
esac
newline

